package net.wangds.redis;

import net.wangds.cache.api.CacheNames;
import net.wangds.log.helper.LogHelper;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
 * redis配置.
 *
 */
@Configuration
@EnableCaching
@Order(0)
public class RedisConfig extends CachingConfigurerSupport implements InitializingBean {

    @Bean
    public KeyGenerator keyGenerator() {
        return (target, method, params) -> {
            StringBuilder sb = new StringBuilder();
            sb.append(target.getClass().getName());
            sb.append(method.getName());
            for (Object obj : params) {
                sb.append(obj.toString());
            }
            return sb.toString();
        };
    }



    @Bean // 关联redis到注解
    @Order(1)
    CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {

        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);

        // 默认配置，过期时间指定是30分钟
        RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig();
        defaultCacheConfig.entryTtl(Duration.ofHours(8));//默认8小时


        Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
        //缓存时间

        redisCacheConfigurationMap.put(CacheNames.ALWAYS, RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ZERO));
        redisCacheConfigurationMap.put(CacheNames.D31, RedisCacheConfiguration.defaultCacheConfig().
                entryTtl(Duration.ofDays(31)));
        redisCacheConfigurationMap.put(CacheNames.D7, RedisCacheConfiguration.defaultCacheConfig().
                entryTtl(Duration.ofDays(7)));
        redisCacheConfigurationMap.put(CacheNames.D5, RedisCacheConfiguration.defaultCacheConfig().
                entryTtl(Duration.ofDays(5)));
        redisCacheConfigurationMap.put(CacheNames.D3, RedisCacheConfiguration.defaultCacheConfig().
                entryTtl(Duration.ofDays(3)));
        redisCacheConfigurationMap.put(CacheNames.D2, RedisCacheConfiguration.defaultCacheConfig().
                entryTtl(Duration.ofDays(2)));
        redisCacheConfigurationMap.put(CacheNames.D1, RedisCacheConfiguration.defaultCacheConfig().
                entryTtl(Duration.ofDays(1)));
        redisCacheConfigurationMap.put(CacheNames.H10, RedisCacheConfiguration.defaultCacheConfig().
                entryTtl(Duration.ofHours(10)));
        redisCacheConfigurationMap.put(CacheNames.H4, RedisCacheConfiguration.defaultCacheConfig().
                entryTtl(Duration.ofHours(4)));
        redisCacheConfigurationMap.put(CacheNames.H3, RedisCacheConfiguration.defaultCacheConfig().
                entryTtl(Duration.ofHours(3)));
        redisCacheConfigurationMap.put(CacheNames.H2, RedisCacheConfiguration.defaultCacheConfig().
                entryTtl(Duration.ofHours(2)));
        redisCacheConfigurationMap.put(CacheNames.H1, RedisCacheConfiguration.defaultCacheConfig().
                entryTtl(Duration.ofHours(1)));
        redisCacheConfigurationMap.put(CacheNames.M30, RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofMinutes(30)));
        redisCacheConfigurationMap.put(CacheNames.M15, RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofMinutes(15)));
        redisCacheConfigurationMap.put(CacheNames.M10, RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofMinutes(10)));
        redisCacheConfigurationMap.put(CacheNames.M5, RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofMinutes(5)));
        redisCacheConfigurationMap.put(CacheNames.M1, RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofMinutes(1)));
        redisCacheConfigurationMap.put(CacheNames.S30, RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofSeconds(30)));
        redisCacheConfigurationMap.put(CacheNames.S15, RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofSeconds(15)));
        redisCacheConfigurationMap.put(CacheNames.S10, RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofSeconds(10)));
        redisCacheConfigurationMap.put(CacheNames.S5, RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofSeconds(5)));

        return new RedisCacheManager(redisCacheWriter, defaultCacheConfig, redisCacheConfigurationMap);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        LogHelper.info("加载redis配置。");
    }
}